
* === Robustness: Sensitivity to fuel prices ===================================

use ${newdata}carownership_dataset_bergen, clear

/* Only keep required obs */
keep if $trmgroup != 0 
keep if $trmgroup != .
keep if 	(year >= $firstyearpre  & year <= $lastyearpre) ///
		  | (year >= $firstyearpost & year <= $lastyearpost)

/* setup variables */
capt drop bergen 		  
gen commuters = ($trmgroup == 1 | $trmgroup == 3) 
gen bergen = 	($trmgroup == 1 | $trmgroup == 2)
gen post = (year >= $firstyearpost & year <= $lastyearpost)
gen bergen_commuters = bergen * commuters
gen commuters_post = commuters * post
gen bergen_commuters_post = bergen_commuters * post

/* All time-varying variables based on geography (residence or work location)
   are set to their 2014 values (variation post 2014 is endogenous to treatment) */
foreach var in dist time_work PublicVSCarTime_fam_mean PublicDiffCarTime_fam_mean grk {
	replace `var' = . if year != 2014
	bysort familienr (`var'): replace `var' = `var'[_n-1] if missing(`var')
}

sort familienr year
egen long famid_num = group(familienr)

* === Run regressions ==========================================================
eststo clear

foreach regvar in bev nrbev ice cars {
	global yvar `regvar'
	
	* Control for fuel prices * grk * year
	reghdfe $yvar ///
		bergen_commuters_post /// DiDiD: post commuter in bergen		
		$xvar /// bev owenership
		i.commuters#i.year /// 
		bergen_commuters /// 
		, absorb( ///
			i.grk#i.year##c.dist /// 
			i.fam_fe ///
		) vce(cluster grk)
		
	eststo reg_$yvar
	summarize $yvar if e(sample) == 1 & year == $lastyearpre & bergen_commuters == 1
	estadd scalar MeanValue1 = r(mean), replace
	summarize $yvar if e(sample) == 1 & year == $lastyearpost & bergen_commuters == 1
	estadd scalar MeanValue2 = r(mean), replace
	eststo reg_$yvar 	
	estimates save ${ster}TableD5_${yvar}, replace	
}

/* === Read estimates from ster, write table to tex ========================= */
eststo drop *

foreach var in bev nrbev ice cars {
	estimates use "${ster}TableD5_`var'" 
	eststo
}
esttab * ///
	using "${tables}tabD5est.tex", ///
	keep(bergen_commuters_post)  ///
	coeflabels( ///
	bergen_commuters_post "Post $\times$ Paying commuters $\times$ Bergen" ///
	) ///
	fragment nodepvars nonumbers nogaps nolines nonotes noobs ///label   nogap booktabs ///
	nomtitle /// 
	prehead("") posthead("") ///
	stats() ///
	se  ///
	star(* 0.10 ** 0.05 *** 0.01)  ///
	replace	substitute(\_ _)

esttab * ///
	using "${tables}tabD5sumstat.tex", ///
	drop(*)  ///
	fragment nodepvars nonumbers nogaps nolines nonotes noobs ///label   nogap booktabs ///
	nomtitle  /// 
	prehead("") posthead("") ///
	stats(N  MeanValue1 MeanValue2, ///
	label("Observations"  "Mean depvar 2014 (paying commuters, Bergen)" "Mean depvar 2017 (paying commuters, Bergen)" ///
	) fmt(0 4 4) ) ///
	se  ///
	star(* 0.10 ** 0.05 *** 0.01)  ///
	replace	substitute(\_ _)